package com.qfedu.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * 公众号：Java架构栈
 * @Author: 卓不凡
 */
@RestController
public class ShowController {
    @RequestMapping("/show/name")
    //获取用户登录的用户名
    public String showName(){
        Object details = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //返回的是UserDetails
        if (details instanceof User){
            User user = (User) details;
            return user.getUsername();
        }
        return details.toString();
    }
}
